/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.gups.guti.camel.etl;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;

/**
 *
 * @author khaled
 */
public class ETLGupsToUpdateProducts {

    private static final String RIVTA_WS_HOSTNAME = "localhost";
    private static final String RIVTA_WS_PORT = "8088";
    private static final String RIVTA_WS_PATH = "/mockUpdateProductsResponderBinding";
    private static final String URL_OFFERS_WSDL = "&wsdlURL=http://localhost:8088/mockUpdateOffersResponderBinding?WSDL";
    private static final String URL_PRODUCTS_WSDL = "&wsdlURL=http://localhost:8088/mockUpdateProductsResponderBinding?WSDL";

    private Main main;
    private static final String URI_EXTRACT_PRODUCTS_FROM_DIR
            = "file:///Users/khaled/test/?move=.camel/${file:name}_${date:now:yyyyMMdd_hhmmss}&noop=false&autoCreate=true";
    private static final String URI_TRANSFORM_PRODUCTS_XSLT = "xslt:file:///Users/khaled/NetBeansProjects/sll-servicecatalogue/import/gups/src/main/resources/META-INF/ProductsXslTransform.xsl";
    private static final String URI_LOAD_UPDATE_PRODUCTS
            = "cxf://http://" + RIVTA_WS_HOSTNAME + ":" + RIVTA_WS_PORT + RIVTA_WS_PATH
            + "?dataFormat=MESSAGE"
            + URL_PRODUCTS_WSDL
            + "&serviceName={urn:riv:service:catalogue:UpdateProducts:1:rivtabp21}UpdateProductsResponderService"
            + "&portName={urn:riv:service:catalogue:UpdateProducts:1:rivtabp21}UpdateProductsResponderPort"
            + "&endpointName={urn:riv:service:catalogue:UpdateProducts:1:rivtabp21}UpdateProducts";

    private static final String URI_EXTRACT_OFFERS_FROM_DIR = "file://tmp/gups/offers/${file:name}_${date:now:yyyyMMdd_hhmmss}&noop=false&autoCreate=true";
    private static final String URI_TRANSFORM_OFFERS_XSLT = "xslt:file:///Users/khaled/NetBeansProjects/sll-servicecatalogue/import/gups/src/main/resources/META-INF/OffersXslTransform.xsl";
    private static final String URI_LOAD_UPDATE_OFFERS
            = "cxf://http://" + RIVTA_WS_HOSTNAME + ":" + RIVTA_WS_PORT + RIVTA_WS_PATH
            + "?dataFormat=MESSAGE"
            + URL_OFFERS_WSDL
            + "&serviceName={urn:riv:service:catalogue:UpdateProducts:1:rivtabp21}UpdateOffersResponderService"
            + "&portName={urn:riv:service:catalogue:UpdateProducts:1:rivtabp21}UpdateOffersResponderPort"
            + "&endpointName={urn:riv:service:catalogue:UpdateProducts:1:rivtabp21}UpdateOffers";

    /**
     *
     * @param args
     * @throws java.lang.Exception
     */
    public static void main(String args[]) throws Exception {
        ETLGupsToUpdateProducts e = new ETLGupsToUpdateProducts();
        e.boot();
    }

    /**
     *
     * @throws Exception
     */
    public void boot() throws Exception {
        // create a Main instance
        main = new Main();
        CamelContext c = main.getOrCreateCamelContext();
        c.setTracing(true);
        c.getProperties().put(Exchange.LOG_DEBUG_BODY_STREAMS, "true");
        main.addRouteBuilder(new GUPSETL());
        // run until you terminate the JVM
        System.out.println("Starting ETL from GUPS/GUTi to UpdateProducts. Use ctrl + c to terminate the JVM.\n");
        main.run();
    }

    /**
     * Routes for loading products and offers from GUPS.
     * 
     */
    private static class GUPSETL extends RouteBuilder {

        @Override
        public void configure() throws Exception {
            // extract products.xml from file, transform to RIVTA UpdateProducts, load to WS
            from(URI_EXTRACT_PRODUCTS_FROM_DIR).routeId("GUPS ETL UpdateProducts")
                    .to(URI_TRANSFORM_PRODUCTS_XSLT)
                    .to(URI_LOAD_UPDATE_PRODUCTS);
            
            // extract offers.xml from file, transform to RIVTA UpdateOffers, load to WS
            from(URI_EXTRACT_OFFERS_FROM_DIR).routeId("GUPS ETL UpdateOffers")
                    .to(URI_TRANSFORM_OFFERS_XSLT)
                    .to(URI_LOAD_UPDATE_OFFERS);

        }
    }
}
